//---------------------------------------------------------------------------
#pragma hdrstop
#include "Shop.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
Shop::Shop(AnsiString id, AnsiString name)
{
	this->id = id;
	this->name = name;
	this->articles = new Article*[this->NUM_ARTICLES];    //make a new pointer of array

	for (int i = 0; i < NUM_ARTICLES; i++)
	{
		this->articles[i] = NULL;
	}
	this->nrOfArticles = 0;
    this->nrOfPurchases = 0;
}
//---------------------------------------------------------------------------
Shop::~Shop()
{
	for (int i = 0; i <= nrOfArticles; i++)
	{
		delete this->articles[i];
	}
	delete [] articles; // delete the array
}
//---------------------------------------------------------------------------
int Shop::getIndexOfArticle(int id)
{
	// loop for the array
	for (int index = 0; index < this->nrOfArticles; index++)  // use nrOfArticles
	{   // check the name of user type and the articles
		if (id == this->articles[index]->getArticleID())
		{
			return index;
		}
	}
	return -1;
}
//---------------------------------------------------------------------------
int Shop::getIndexOfPurchase(int id)
{
	// loop for the array
	for (int index = 0; index < this->nrOfPurchases; index++)  // use nrOfPurchases
	{   // check the name of user type and the articles
		if (id == this->purchaseIdList[index])
		{
			return index;
		}
	}
	return -1;
}
//---------------------------------------------------------------------------
AnsiString Shop::getID()		{ return this->id; }
//---------------------------------------------------------------------------
AnsiString Shop::getName()      { return this->name; }
//---------------------------------------------------------------------------
int Shop::getCapacity()			{ return this->NUM_ARTICLES; }
//---------------------------------------------------------------------------
int Shop::getNrOfArticles()     { return nrOfArticles; }
//---------------------------------------------------------------------------
int Shop::getNrOfPurchases()	{ return this->nrOfPurchases; }
//---------------------------------------------------------------------------
Article* Shop::getArticleAtIndex(int i) { return this->articles[i]; }
//---------------------------------------------------------------------------
Article* Shop::getPurchaseAtIndex(int i)
{
	int id = this->purchaseIdList[i];	 // get the id from the purchase
	int index = getIndexOfArticle(id);   // get article index with the id
	return this->articles[index];		 // return the article
}
//---------------------------------------------------------------------------
int Shop::getPurchaseIdAtIndex(int i)
{
    return this->purchaseIdList[i];		// return the id
}
//---------------------------------------------------------------------------
int Shop::getPurchaseQtAtIndex(int i)
{
    return this->purchaseQtList[i];	   // return the quantity
}
//---------------------------------------------------------------------------
bool Shop::addArticle(int id, AnsiString nm, int amount, AnsiString kind, double price, double deposit)
{
	if (this->nrOfArticles < this->NUM_ARTICLES) // check if there is enough space
	{
		this->articles[this->nrOfArticles] = new Article(id, nm, amount, kind, price, deposit);
		this->nrOfArticles++;
		return true;
	}
	else
	{
		return false;
	}
}
//---------------------------------------------------------------------------
bool Shop::soldSome(int id, int amount)
{
	int artIndex = this->getIndexOfArticle(id); // get index
	if (artIndex != -1) // if it is exist
	{
		// if stock is bigger than what they want to remove
		if (this->articles[artIndex]->getNrOfItems() >= amount)
		{
        	// decrease the stock
			this->articles[artIndex]->decreaseNrOfItemsBy(amount);

			// then add this amount to the purchase
			int purIndex = this->getIndexOfPurchase(id);  // get index
			if (purIndex != -1) // if it is exist
			{
				// increase quantity
				this->purchaseQtList[purIndex] += amount;
			}
			else
			{
				// add to purchase
				this->purchaseIdList[this->nrOfPurchases] = id;
				this->purchaseQtList[this->nrOfPurchases] = amount;
				this->nrOfPurchases++;	// increase nrOfPurchases
			}
			return true;
		}
	}
	return false;
}
//---------------------------------------------------------------------------
bool Shop::unsoldSome(int id, int amount)
{
	int artIndex = this->getIndexOfArticle(id); // get index
	if (artIndex != -1) // if it is exist
	{
		// remove amount from purchase
		int purIndex = this->getIndexOfPurchase(id);  // get index
		// check how much we want to remove
		if (amount < this->purchaseQtList[purIndex])
		{
			 // increase the stock
			 this->articles[artIndex]->increaseNrOfItemsBy(amount);

			 // remove some
			 this->purchaseQtList[purIndex] -= amount;
		}
		else
		{
			// increase the stock
			this->articles[artIndex]->increaseNrOfItemsBy(this->purchaseQtList[purIndex]);
			
			// remove all
			for (int i = purIndex; i < this->nrOfPurchases-1; i++)
			{
				this->purchaseIdList[i] = this->purchaseIdList[i+1];
				this->purchaseQtList[i] = this->purchaseQtList[i+1];
			}
			this->nrOfPurchases--;  // decrease nrOfPurchases
		}
		return true;
	}
	return false;
}
//---------------------------------------------------------------------------
void Shop::unsoldAll()
{
	// clear the whole purchase
	for (int purIndex = 0; purIndex < this->nrOfPurchases; purIndex++)
	{
		// get the article index with the purchase id
		int artIndex = this->getIndexOfArticle(this->purchaseIdList[purIndex]);
		// increase the stock
		this->articles[artIndex]->increaseNrOfItemsBy(this->purchaseQtList[purIndex]);
	}
	this->nrOfPurchases = 0;	// reset purchases quantity
}
//---------------------------------------------------------------------------
void Shop::clearAllPuchases()		{ this->nrOfPurchases = 0; }
//---------------------------------------------------------------------------
double Shop::getTotalPrice()
{
	double total = 0.0;
	for (int i = 0; i < this->nrOfPurchases; i++)
	{
    	// get the price of the article with id of the purchase at index i and times it by the quantity
    	total += articles[getIndexOfArticle(this->purchaseIdList[i])]->getPrice() * this->purchaseQtList[i];
	}
	return total;		// return the total price
}
//---------------------------------------------------------------------------
void Shop::sortAllArticlesByName()
{
	for (int count = 0; count < this->nrOfArticles; count++)
	{
		int smallest = count;
		for (int index = count+1; index < this->nrOfArticles; index++)
		{
		if (this->articles[smallest]->getName() > this->articles[index]->getName())
		   {
				smallest = index;
		   }
		}
		Article* temp;
		temp = articles[count];
		this->articles[count] = this->articles[smallest];
		this->articles[smallest] = temp;
	}
}
//---------------------------------------------------------------------------
void Shop::sortAllArticlesBySalesValue()
{
	for (int count = 0; count < this->nrOfArticles; count++)
	{
		int smallest = count;
		for (int index = count+1; index < this->nrOfArticles; index++)
		{
			if ((this->articles[smallest]->getPrice()*this->articles[smallest]->getNrOfItems()) > (this->articles[index]->getPrice()*this->articles[index]->getNrOfItems()))
			{
				smallest = index;
			}
		}
		Article* temp;
		temp = this->articles[count];
		this->articles[count] = this->articles[smallest];
		this->articles[smallest] = temp;
	}
}
//---------------------------------------------------------------------------
void Shop::sortAllArticlesByKind()
{
	sortAllArticlesByName();
	for (int count = 0; count < this->nrOfArticles; count++)
	{
		int smallest = count;
		for (int index = count+1; index < this->nrOfArticles; index++)
		{
		if (this->articles[smallest]->getKind() > this->articles[index]->getKind())
		   {
				smallest = index;
		   }
		}
		Article* temp;
		temp = articles[count];
		this->articles[count] = this->articles[smallest];
		this->articles[smallest] = temp;
	}
}
//---------------------------------------------------------------------------
